import { createSlice } from '@reduxjs/toolkit'
const todoListSlice = createSlice({
    name: 'todoList',
    initialState: {
        list: [],
        recycleBin: [],
        key: 'all'
    },
    reducers: {
        addTodoList(state, { payload }) {
            state.list = [...state.list, payload]
        },
        changeItem(state, { payload }) {
            state.list = state.list.map(item => {
                if (item.id === payload) {
                    item.isDone = !item.isDone
                }
                return item
            })
        },
        deleteItem(state, { payload }) {
            state.recycleBin = [...state.recycleBin, ...state.list.filter(item => item.id === payload)]
            state.list = state.list.filter(item => item.id !== payload)
        },
        changeBtn(state, { payload }) {
            state.key = payload
        },
        removeRecycleBin(state, { payload }) {
            state.recycleBin = [...state.recycleBin.filter(item => item.id !== payload)]
        },
        toTodoList(state, { payload }) {
            state.list = [...state.list, payload]
            state.recycleBin = [...state.recycleBin.filter(item => item.id !== payload.id)]
        }
    }

})
export const { addTodoList, changeItem, deleteItem, changeBtn, removeRecycleBin, toTodoList } = todoListSlice.actions
export default todoListSlice.reducer